package wineshop.controller.backend;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import wineshop.domain.Hotel;
import wineshop.domain.Room;
import wineshop.domain.RoomSelect;
import wineshop.exception.ApplicationException;
import wineshop.service.HotelService;
import wineshop.service.RoomQueryHelper;
import wineshop.service.RoomService;
import wineshop.service.Impl.HotelServiceImpl;
import wineshop.service.Impl.RoomServiceImpl;
import wineshop.utils.Page;

public class RoomServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");

		String act = request.getParameter("act");

		if ("input_room".equals(act)) {

			HotelService hotelService = new HotelServiceImpl();
			request.setAttribute("hotels", hotelService.GetAllHotel());
			request.getRequestDispatcher("jsps/backend/room/input_room.jsp")
					.forward(request, response);

		} else if ("CreateRoom".equals(act)) {
			Hotel hotel = new Hotel();
			Room room = new Room();

			int hotelId = Integer.parseInt(request.getParameter("hotelId"));
			HotelService hotelService = new HotelServiceImpl();
			hotel = hotelService.GetHotelById(hotelId);

			room.setHotel(hotel);
			room.setRoomNumber(Integer.parseInt(request
					.getParameter("roomnumber")));
			room.setRoomType(request.getParameter("roomtype"));
			room.setRoomFacility(request.getParameterValues("roomfacility"));
			room.setRoomStatus(request.getParameter("roomstatus"));
			room.setRoomDesc(request.getParameter("roomdesc"));
			
			
			RoomService roomService = new RoomServiceImpl();
			try {
				roomService.CreateRoom(room);
				response.sendRedirect("RoomMgr?act=qryloadall");
			} catch (ApplicationException e) {
				e.printStackTrace();
				request.setAttribute("errMsg", e.getMessage());
				request.getRequestDispatcher("jsps/backend/room/input_room.jsp").forward(request,response);
			}

		}else if("qryloadall".equals(act)){
			RoomQueryHelper helper = new RoomQueryHelper();
			
			if(request.getParameter("qryHotelName")!=null && Integer.parseInt(request.getParameter("qryHotelName"))!=0)
			    helper.setQryHotelName(request.getParameter("qryHotelName"));
			if(request.getParameter("qryRoomType")!=null && !request.getParameter("qryRoomType").equals("无"))
			    helper.setQryRoomType(request.getParameter("qryRoomType"));
			if(request.getParameter("qryRoomStatus")!=null && !request.getParameter("qryRoomStatus").equals("无"))
			    helper.setQryRoomStatus(request.getParameter("qryRoomStatus"));
			
			Page page=new Page();
			if(request.getParameter("pageNo")!=null)
				page.setPageNo(Integer.parseInt(request.getParameter("pageNo")));
			

			RoomService roomService=new RoomServiceImpl();
			
			//helper未指定，也就是说，所有的属性为null, 则分页就是对所有记录的分页。
			//如果helper有指定属性，则分页，是基于组合查询基础上的分页。
	        request.setAttribute("page",roomService.loadRoomsByCondition(helper,page));
	        
	        HotelService hotelService = new HotelServiceImpl();
			request.setAttribute("hotels", hotelService.GetAllHotel());
			
	        RoomSelect rs = new RoomSelect();
			request.setAttribute("rs", rs);
			request.getRequestDispatcher("jsps/backend/room/list_room.jsp").forward(request, response);
			
				
		}else if("preModify".equals(act)){
			int roomId = Integer.parseInt(request.getParameter("roomId"));
			Room room = null;
			
			RoomService roomService = new RoomServiceImpl();
			room = roomService.GetRoomById(roomId);
			
			request.setAttribute("room", room);
			RoomSelect rs = new RoomSelect();
			request.setAttribute("rs", rs);
			request.getRequestDispatcher("jsps/backend/room/update_room.jsp").forward(request, response);
		}else if("update".equals(act)){
			Room room = new Room();
			Hotel hotel = new Hotel();
			room.setRoomId(Integer.parseInt(request.getParameter("roomId")));
			
			int hotelId = Integer.parseInt(request.getParameter("hotelId"));
			HotelService hotelService = new HotelServiceImpl();
			hotel = hotelService.GetHotelById(hotelId);
			room.setHotel(hotel);
			room.setRoomNumber(Integer.parseInt(request
					.getParameter("roomnumber")));
			room.setRoomType(request.getParameter("roomtype"));
			room.setRoomFacility(request.getParameterValues("roomfacility"));
			room.setRoomStatus(request.getParameter("roomstatus"));
			room.setRoomDesc(request.getParameter("roomdesc"));
			
			
			RoomService roomService = new RoomServiceImpl();
			try {
				roomService.UpdateRoom(room);
				response.sendRedirect("RoomMgr?act=qryloadall");
			} catch (ApplicationException e) {
				e.printStackTrace();
				request.setAttribute("errMsg", e.getMessage());
				RoomSelect rs = new RoomSelect();
				request.setAttribute("rs", rs);
				request.setAttribute("room", room);
				request.getRequestDispatcher("jsps/backend/room/update_room.jsp").forward(request,response);
			}
			
		}else if("remove".equals(act)){
	
			int roomId=Integer.parseInt(request.getParameter("roomId"));
			
			RoomService roomService=new RoomServiceImpl();
			
			try {
				roomService.removeRoom(roomId);
				request.getSession().setAttribute("errMsg", "房间删除成功！");
			} catch (ApplicationException e) {
				e.printStackTrace();
				request.getSession().setAttribute("errMsg", e.getMessage());
			}
			
			
			response.sendRedirect("RoomMgr?act=qryloadall");
		}
	}
		
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doGet(request, response);
	}

}
